﻿using HR.Config;
using HR.IService;
using HR.Models;

namespace HR.Service
{
    public class Second_kindService : ISecond_kindService
    {
        private readonly HRDBContext db;

        public Second_kindService(HRDBContext db)
        {
            this.db = db;
        }
        public Task<Paging<config_file_second_kind>> SecondKindPage()
        {
            return Task.Run(() =>
            {
                List<config_file_second_kind> tabs = db.config_file_second_kinds.ToList();
                Paging<config_file_second_kind> pg = new Paging<config_file_second_kind>()
                {
                    tabs = tabs,
                    rows = tabs.Count
                };
                return pg;
            });
        }
        public Task<int> AddSecondKind(config_file_second_kind fsk)
        {
            return Task.Run(() =>
            {
                //倒序，获取到最大的II级机构编号
                List<config_file_second_kind> list = db.config_file_second_kinds.ToList().OrderByDescending(ffk => ffk.second_kind_id).ToList();
                //将II级机构编号强转成Int类型，以便递增操作
                int skid = 0;
                int.TryParse(list[0].second_kind_id, out skid);
                //对最大值递增+1，然后判断字符串长度是否满足2，不够往左侧添加0
                string cfskid = (skid + 1).ToString().PadLeft(2,'0');
                //将对象的skid赋值
                fsk.second_kind_id = cfskid;
                db.config_file_second_kinds.AddAsync(fsk);
                return db.SaveChanges(); 
            });
        }


        public Task<int> UdpSecondKind(config_file_second_kind fsk)
        {
            return Task.Run(() =>
            {
                config_file_second_kind? cfsk = db.config_file_second_kinds.Find(fsk.fsk_id);
                cfsk.first_kind_id = fsk.first_kind_id;
                cfsk.first_kind_name = fsk.first_kind_name;
                cfsk.second_kind_name = fsk.second_kind_name;
                cfsk.second_salary_id = fsk.second_salary_id;
                cfsk.second_sale_id = fsk.second_sale_id;
                return db.SaveChanges();
            });
        }
        public Task<int> DelSecondKind(short fskid)
        {
            return Task.Run(() =>
            {
                config_file_second_kind? cfsk = db.config_file_second_kinds.Find(fskid);
                db.config_file_second_kinds.Remove(cfsk);
                return db.SaveChanges();

            });
        }
    }
}
